import tech.mhuang.pacebox.core.util.StringUtil;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class SqlTemplate {

    protected void generatorTimestampSql(StringBuilder sqlBuilder, Timestamp param, boolean checked) throws SQLException {
        if (param != null) {
            sqlBuilder.append("'" + param + "'");
        } else {
            sqlBuilder.append("null");
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorDecimalSql(StringBuilder sqlBuilder, BigDecimal param, boolean checked) throws SQLException {
        if (param != null) {
            sqlBuilder.append(param);
        } else {
            sqlBuilder.append(0);
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorStringSql(StringBuilder sqlBuilder, String value, boolean checked) {
        if (StringUtil.isEmpty(value)) {
            generatorNullSql(sqlBuilder, checked);
        } else {
            generatorComValueSql(sqlBuilder, value, checked);
        }
    }

    protected void generatorNullSql(StringBuilder sqlBuilder, boolean checked) {
        sqlBuilder.append("null");
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorComValueSql(StringBuilder sqlBuilder, String value, boolean checked) {
        sqlBuilder.append("'" + value + "'");
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorDecimalSql(ResultSet rs, String param, StringBuilder sqlBuilder, boolean checked) throws SQLException {
        if (rs.getBigDecimal(param) != null) {
            sqlBuilder.append(rs.getBigDecimal(param));
        } else {
            sqlBuilder.append(0);
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void genertorStringSql(ResultSet rs, String param, StringBuilder sqlBuilder, boolean checked) throws SQLException {
        if (rs.getString(param) != null) {
            sqlBuilder.append("'" + rs.getString(param) + "'");
        } else {
            sqlBuilder.append(rs.getString(param));
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorTimestampSql(ResultSet rs, String param, StringBuilder sqlBuilder, boolean checked) throws SQLException {
        if (rs.getTimestamp(param) != null) {
            sqlBuilder.append("'" + rs.getTimestamp(param) + "'");
        } else {
            sqlBuilder.append("null");
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }

    protected void generatorDateSql(ResultSet rs, String param, StringBuilder sqlBuilder, boolean checked) throws SQLException {
        if (rs.getDate(param) != null) {
            sqlBuilder.append("'" + rs.getDate(param) + "'");
        } else {
            sqlBuilder.append("null");
        }
        if (checked) {
            sqlBuilder.append(",");
        }
    }
}
